package cn.thrjit.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.thrjit.mapper.DriverAuthMaterialMapper;
import cn.thrjit.pojo.domain.Driver;
import cn.thrjit.pojo.domain.DriverAuthMaterial;
import cn.thrjit.pojo.domain.DriverMaterialAuthLog;
import cn.thrjit.pojo.dto.DriverRealAuthAuditDto;
import cn.thrjit.pojo.query.PageQueryWrapper;
import cn.thrjit.rocketmq.GlobalExceptionCode;
import cn.thrjit.service.IDriverAuthMaterialService;
import cn.thrjit.service.IDriverMaterialAuthLogService;
import cn.thrjit.service.IDriverService;
import cn.thrjit.utils.AssertUtil;
import cn.thrjit.utils.BitStatesUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-07-27
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
    @Autowired
    private IDriverService driverService;
    @Autowired
    private IDriverMaterialAuthLogService driverMaterialAuthLogService;
    /**
     * 实名认证
     * @param driverAuthMaterial
     */
    @Override
    public void saveRealAuth(DriverAuthMaterial driverAuthMaterial) {
        // 参数校验 通过jos303框架校验参数
        // 业务校验
        // 校验之前是否提交过实名认证
        long driverId = StpUtil.getLoginIdAsLong();
        List<DriverAuthMaterial> list = super.list(new LambdaQueryWrapper<DriverAuthMaterial>()
                // 条件获取数据库的driverId要和当前登录的driverId一致
                .eq(DriverAuthMaterial::getDriverId, driverId)
                // 条件获取数据库的realAuthStatus要为0或1
                .in(DriverAuthMaterial::getRealAuthStatus, Arrays.asList(0, 1)));
        // 如果list不为空，说明之前已经提交过实名认证，不允许再次提交实名认证
        AssertUtil.isTrue(CollUtil.isEmpty(list), GlobalExceptionCode.PARAM_ERROR);
        // 业务实现,保存实名认证信息，修改位状态
        driverAuthMaterial.setRealAuthStatus(0);
        driverAuthMaterial.setDriverId(driverId);
        driverAuthMaterial.setCreateTime(new Date());
        driverAuthMaterial.setDeleted(false);
        driverAuthMaterial.setVersion(0);
        super.save(driverAuthMaterial);
        Driver driver = driverService.getById(driverAuthMaterial.getDriverId());
        AssertUtil.isNotNull(driver,GlobalExceptionCode.PARAM_ERROR);
        // 修改位状态
        driver.setBitState(BitStatesUtil.addState(driver.getBitState(), BitStatesUtil.OP_REAL_AUTHENTICATING));
        // 更新driver
        driverService.updateById(driver);
    }

    /**
     * 司机审核信息
     * @param
     */
    @Override
    public void realauth(DriverRealAuthAuditDto driverRealAuthAuditDto) {
        // 参数校验 通过jos303框架校验参数
        // 业务校验
        // 校验实名信息是否存在
        Long id = driverRealAuthAuditDto.getId();// 拿到审核信息的id
        DriverAuthMaterial driverAuthMaterial = super.getById(id);// 根据id获取实名认证信息对象
        AssertUtil.isNotNull(driverAuthMaterial,GlobalExceptionCode.PARAM_ERROR);// 实名认证信息对象不能为空
        // 校验是否已经审核过
        Long driverId = driverAuthMaterial.getDriverId();// 获取到当前对象的司机id
        DriverAuthMaterial authMaterial = super.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, driverId)// 判断如果数据库DriverId和当前登录的DriverId一致
                .eq(DriverAuthMaterial::getRealAuthStatus, 1));//并且realAuthStatus为1，则说明已经审核过
        // 如果authMaterial不为空，说明已经审核过，不允许再次审核
        AssertUtil.isNull(authMaterial,GlobalExceptionCode.PARAM_ERROR);
        // 业务实现,修改实名认证信息的审核状态
        Boolean approve = driverRealAuthAuditDto.getApprove();// 拿到前端给的状态
        String remark = driverRealAuthAuditDto.getRemark();// 拿到前端给的备注
        Date date = new Date();// 获取当前时间
        long loginIdAsLong = StpUtil.getLoginIdAsLong();// 审核人的id
        Driver driver = driverService.getById(driverId);// 通过实名认证信息的driverId获取司机对象
        if (approve) {// 说明审核通过
            driverAuthMaterial.setRealAuthStatus(1);// 修改实名认证信息的审核状态为1
            // 修改位状态当前位状态加上审核通过的位状态
            driver.setBitState(BitStatesUtil.addState(driver.getBitState(), BitStatesUtil.OP_REAL_AUTHENTICATIONED));
        }else {// 说明审核不通过
            driverAuthMaterial.setRealAuthStatus(2);// 修改实名认证信息的审核状态为2
            // 修改位状态当前位状态减去审核中的位状态
            driver.setBitState(BitStatesUtil.removeState(driver.getBitState(), BitStatesUtil.OP_REAL_AUTHENTICATING));
        }
        // 手动设置更新时间，审核时间，审核人id，审核备注
        driverAuthMaterial.setUpdateTime(date);
        driverAuthMaterial.setAuditTime(date);
        driverAuthMaterial.setAuditUserId(loginIdAsLong);
        driverAuthMaterial.setAuditRemark(remark);
        super.updateById(driverAuthMaterial);

        // 更新审核日志
        // 把实名认证信息拷贝给为DriverMaterialAuthLog对象并接收
        DriverMaterialAuthLog driverMaterialAuthLog = BeanUtil.copyProperties(driverAuthMaterial, DriverMaterialAuthLog.class);
        driverMaterialAuthLog.setId(null);// 手动清空id
        driverMaterialAuthLog.setAuthMaterialId(id);
        driverMaterialAuthLog.setCreateTime(date);
        driverMaterialAuthLog.setDeleted(false);
        driverMaterialAuthLog.setVersion(0);
        driverMaterialAuthLogService.save(driverMaterialAuthLog);
    }
}
